package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.adapter.FriendListAdapter;
import a66weding.com.jiehuntong.adapter.MyTaskBAdapter;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MarriageProposalBean;
import a66weding.com.jiehuntong.beans.MyTaskBean;
import a66weding.com.jiehuntong.beans.ReleaseTaskBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
import a66weding.com.jiehuntong.widgets.DividerItemDecoration;

/**
 * Created by admin on 2016/7/23.
 * 任务列表界面;
 */
public class FriendshelptaskActivity extends Activity implements OnNetResultListener,OnItemClickLitener {

    private TextView task_tv;
    private TextView title;
    private XRefreshView xRefreshView;
    private List<ReleaseTaskBean.DataBean> relaseTaskDatalist;
    private MyTaskBAdapter adapter;
    private String id;
    private RecyclerView recyclerView;
    private boolean isDownRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_a);
        id = getIntent().getStringExtra("_id");
        initView();
        initData();

    }

    private void initView(){
        task_tv = (TextView) findViewById(R.id.back_iamge);
        task_tv.setText("我的帮友");
        title = (TextView) findViewById(R.id.title_tv);
        title.setText("任务");
        initRefreshView();
    }

    private void initData(){
        getNetData();
    }

    public void back(View view){
        finish();
    }

    /**
     * POST Wedding/return_MyTask?Token={Token}&i_id={i_id}
     我的任务
     */
    private void getNetData(){

        String url = Url.SURL + "/Wedding/return_MyTask?Token=00&i_Id="+id;
        HashMap<String,String> paramsMap=new HashMap<>();
        HttpRequestBean requestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, this,ReleaseTaskBean.class, RequestCode.RETURN_MYTASK,paramsMap);
        HttpHandleUtil.executeCommonPost(this, requestBean);
    }

    private ReleaseTaskBean bean;
    private List<ReleaseTaskBean.DataBean> ReleaseTaskBeanlist;
    private  ReleaseTaskBean.DataBean dataBean;
    @Override
    public void onSucess(SuccessBean sucessbean) {
        if (isDownRefresh){
            xRefreshView.stopRefresh();
        }else{
            xRefreshView.stopLoadMore();
        }
        Object data = sucessbean.getData();
        if(data instanceof  ReleaseTaskBean){
            bean = (ReleaseTaskBean) data;
            switch (bean.getResCode()){
                case "0000":
                    ReleaseTaskBeanlist = bean.getData();
                    if(isDownRefresh){
                        adapter.clear();
                    }
                    adapter.append(ReleaseTaskBeanlist);
                    break;
            }
        }

    }

    @Override
    public void onError(ErrorBean errorBean) {
    }


    //初始化上啦刷新
    private void initRefreshView() {
        xRefreshView = (XRefreshView)findViewById(R.id.activity_task_a_x_view);
        //是否允许下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        xRefreshView.setAutoLoadMore(true);
        recyclerView = (RecyclerView)findViewById(R.id.activity_task_a_recycler);
        recyclerView.setHasFixedSize(true);
        //线性布局

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new MyTaskBAdapter(this,null);
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh() {
                isDownRefresh = true;
                getNetData();

            }

            @Override
            public void onLoadMore(boolean isSlience) {
                isDownRefresh = false;
                getNetData();
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(this,JobDetailActivity.class);
        dataBean = ReleaseTaskBeanlist.get(position);
        changeData(dataBean);
        intent.putExtra("flag",4);
        intent.putExtra("headpictrue",""+dataBean.getPicture());
        intent.putExtra(JobDetail.TASK_FLAG,MarriageBean);
        startActivity(intent);
    }

    private MarriageProposalBean.DataBean MarriageBean;
    private void changeData(ReleaseTaskBean.DataBean dataBean){
        MarriageBean = new MarriageProposalBean.DataBean();
        MarriageBean.setParticipant(dataBean.getParticipant());
        MarriageBean.setIsComplete(dataBean.isIsComplete());
        MarriageBean.setBookTitle(dataBean.getBookTitle());
        MarriageBean.setBookPlace(dataBean.getBookPlace());
        MarriageBean.setDays(dataBean.getDays());
        MarriageBean.setNotice(dataBean.getNotice());
        MarriageBean.setRelationName(dataBean.getRelationName());
        MarriageBean.setBudgetMoney(dataBean.getBudgetMoney());
        //备注
        MarriageBean.setRemarks(dataBean.getRemarks());
        //真实姓名
        MarriageBean.setNvc_RealName(dataBean.getNvc_RealName());
    }
}
